package com.museum.guide.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.museum.guide.common.result.Result;
import com.museum.guide.dto.GuideDTO;
import com.museum.guide.entity.Guide;
import com.museum.guide.service.GuideService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 讲解员控制器
 */
@Slf4j
@RestController
@RequestMapping("/guide")
public class GuideController {

    @Autowired
    private GuideService guideService;

    /**
     * 分页查询讲解员列表
     *
     * @param page     页码
     * @param pageSize 每页条数
     * @param name     讲解员名称
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<IPage<Guide>> page(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String name) {
        log.info("分页查询讲解员列表, page: {}, pageSize: {}, name: {}", page, pageSize, name);
        return Result.success(guideService.getPage(page, pageSize, name));
    }

    /**
     * 根据ID查询讲解员
     *
     * @param id 讲解员ID
     * @return 讲解员信息
     */
    @GetMapping("/{id}")
    public Result<Guide> getById(@PathVariable Long id) {
        log.info("根据ID查询讲解员, id: {}", id);
        return Result.success(guideService.getById(id));
    }

    /**
     * 新增讲解员
     *
     * @param guideDTO 讲解员DTO
     * @return 结果
     */
    @PostMapping
    public Result<String> save(@RequestBody @Valid GuideDTO guideDTO) {
        log.info("新增讲解员: {}", guideDTO);
        return Result.success(guideService.save(guideDTO));
    }

    /**
     * 修改讲解员
     *
     * @param guideDTO 讲解员DTO
     * @return 结果
     */
    @PutMapping
    public Result<String> update(@RequestBody @Valid GuideDTO guideDTO) {
        log.info("修改讲解员: {}", guideDTO);
        return Result.success(guideService.update(guideDTO));
    }

    /**
     * 删除讲解员
     *
     * @param id 讲解员ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除讲解员, id: {}", id);
        return Result.success(guideService.remove(id));
    }

    /**
     * 获取所有讲解员列表
     *
     * @return 讲解员列表
     */
    @GetMapping("/list")
    public Result<List<Guide>> list() {
        log.info("获取所有讲解员列表");
        return Result.success(guideService.listAll());
    }
}